package com.kakame.ahasekai.common.util;

import java.util.ArrayList;
import java.util.List;

import com.kakame.ahasekai.common.AppConst;

public class DbUtil {
	/**
	 * 分隔list
	 * @author ex-hejm
	 * @date 2018年12月6日下午3:11:26
	 * @param tList
	 * @param sizeLimit
	 * @return
	 */
	public static <T> List<List<T>> spliceList(List<T> tList, int sizeLimit) {
		// 不要将原来的List改变
		List<T> list = new ArrayList<>();
		list.addAll(tList);
		
		List<List<T>> result = new ArrayList<>();
		if(sizeLimit < 1) {
			sizeLimit = AppConst.BATCH_RECORD_COUNT;
		}
		
		if (list == null || list.size() == 0) {
			return result;
		}
		int size = list.size();
		if (size > sizeLimit) {
			int part = size / sizeLimit;
			int last = size % sizeLimit;
			for (int j = 0; j < part; j++) {
				List<T> subList = list.subList(0, sizeLimit);
				result.add(new ArrayList<>(subList));
				list.subList(0, sizeLimit).clear();
			}
			if (last > 0) {
				List<T> subList = list.subList(0, last);
				result.add(new ArrayList<>(subList));
				list.subList(0, last).clear();
			}
		} else {
			result.add(list);
		}
		return result;
	}
	
}
